{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3cdde5c0-0cdd-4ea8-a465-8c0147318867",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7355b8e1-e4f2-41c8-99fa-d332c515f3e8",
   "metadata": {},
   "source": [
    "# 创建一个series对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "119754bb-754c-4bcc-83bf-faf1394de5c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     语文\n",
       "1     数学\n",
       "2     英语\n",
       "3    计算机\n",
       "dtype: object"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "courses = [\"语文\", \"数学\", \"英语\", \"计算机\"]\n",
    "s = pd.Series(courses)\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d514a2b2-a19f-44d2-95fe-5bdb188234fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文      80\n",
       "数学      90\n",
       "英语      70\n",
       "计算机    100\n",
       "dtype: int64"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grades = {\"语文\":80, \"数学\":90, \"英语\":70, \"计算机\":100}\n",
    "s = pd.Series(grades)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2d81631-fbce-4e31-bcc2-2c698d48efc7",
   "metadata": {},
   "source": [
    "# 创建一个Dataframe对象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b72bca76-2706-4d89-9c85-4583889e2ff1",
   "metadata": {},
   "source": [
    "- 将字典的数据转换为df对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "cc400469-3e71-4adb-8ed2-c8451f0a1b09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>科目</th>\n",
       "      <th>成绩</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>语文</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>数学</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>英语</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>计算机</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    科目   成绩\n",
       "0   语文   80\n",
       "1   数学   90\n",
       "2   英语   70\n",
       "3  计算机  100"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame.from_dict(grades,  orient=\"index\",columns=[\"成绩\"]).reset_index()\n",
    "df.columns = [\"科目\", \"成绩\"]\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc84bdef-8a3f-475a-8252-b8be3a6234bb",
   "metadata": {},
   "source": [
    "# 利用numpy 创建Series对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "7141c18e-656e-4f83-aa83-3221d04191cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "a1f013ae-e38d-4f06-8b33-338b7cc5aaa5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "101    10\n",
       "102    20\n",
       "103    30\n",
       "104    40\n",
       "105    50\n",
       "106    60\n",
       "107    70\n",
       "108    80\n",
       "109    90\n",
       "dtype: int64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(data=np.arange(10, 100, 10), index=np.arange(101, 110))\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09af925e-e812-4e4b-8ef4-3474d3a6f3f4",
   "metadata": {},
   "source": [
    "# 转换数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "35119303-47b7-4809-8a4d-5093edc7367b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(data=[\"001\",\"002\",\"003\",\"004\"], index=list(\"abcd\"))\n",
    "s = s.astype(int)\n",
    "s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "664875bf-357b-4e78-9473-1f2f0652e4ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>性别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>小张</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>小王</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>小李</td>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>小赵</td>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   姓名 性别\n",
       "0  小张  男\n",
       "1  小王  女\n",
       "2  小李  男\n",
       "3  小赵  女"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {\"姓名\":[\"小张\",\"小王\",\"小李\",\"小赵\"], \"性别\":[\"男\",\"女\",\"男\",\"女\"]}\n",
    "df = pd.DataFrame.from_dict(data)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "a613472c-417e-4811-94e8-c0f5d33aeadc",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.set_index(\"姓名\", inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "4141b976-3afb-4542-bc5f-a85c09a00af4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>性别</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>姓名</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>小张</th>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小王</th>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小李</th>\n",
       "      <td>男</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小赵</th>\n",
       "      <td>女</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   性别\n",
       "姓名   \n",
       "小张  男\n",
       "小王  女\n",
       "小李  男\n",
       "小赵  女"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c16e8211-3c19-46d6-ae3d-86ea76952a4f",
   "metadata": {},
   "source": [
    "# 生成一个月份的所有天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "2969a7fa-9149-4edd-aa97-b6c396fc01d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04',\n",
       "               '2021-10-05', '2021-10-06', '2021-10-07', '2021-10-08',\n",
       "               '2021-10-09', '2021-10-10', '2021-10-11', '2021-10-12',\n",
       "               '2021-10-13', '2021-10-14', '2021-10-15', '2021-10-16',\n",
       "               '2021-10-17', '2021-10-18', '2021-10-19', '2021-10-20',\n",
       "               '2021-10-21', '2021-10-22', '2021-10-23', '2021-10-24',\n",
       "               '2021-10-25', '2021-10-26', '2021-10-27', '2021-10-28',\n",
       "               '2021-10-29', '2021-10-30', '2021-10-31'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start=\"2021-10-1\", end=\"2021-10-31\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "4d0ddde5-69de-40b2-ac62-fb4e404d0adc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04',\n",
       "               '2021-10-05', '2021-10-06', '2021-10-07', '2021-10-08',\n",
       "               '2021-10-09', '2021-10-10', '2021-10-11', '2021-10-12',\n",
       "               '2021-10-13', '2021-10-14', '2021-10-15', '2021-10-16',\n",
       "               '2021-10-17', '2021-10-18', '2021-10-19', '2021-10-20',\n",
       "               '2021-10-21', '2021-10-22', '2021-10-23', '2021-10-24',\n",
       "               '2021-10-25', '2021-10-26', '2021-10-27', '2021-10-28',\n",
       "               '2021-10-29', '2021-10-30', '2021-10-31'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start=\"2021-10-1\", periods=31)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "817802c4-b8db-45f5-8c2d-769ac5c4565e",
   "metadata": {},
   "source": [
    "# 生成一年中所有周一的日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "b3aff2a0-fa26-4b99-b0f2-80f0528a07b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-04', '2021-01-11', '2021-01-18', '2021-01-25',\n",
       "               '2021-02-01', '2021-02-08', '2021-02-15', '2021-02-22',\n",
       "               '2021-03-01', '2021-03-08', '2021-03-15', '2021-03-22',\n",
       "               '2021-03-29', '2021-04-05', '2021-04-12', '2021-04-19',\n",
       "               '2021-04-26', '2021-05-03', '2021-05-10', '2021-05-17',\n",
       "               '2021-05-24', '2021-05-31', '2021-06-07', '2021-06-14',\n",
       "               '2021-06-21', '2021-06-28', '2021-07-05', '2021-07-12',\n",
       "               '2021-07-19', '2021-07-26', '2021-08-02', '2021-08-09',\n",
       "               '2021-08-16', '2021-08-23', '2021-08-30', '2021-09-06',\n",
       "               '2021-09-13', '2021-09-20', '2021-09-27', '2021-10-04',\n",
       "               '2021-10-11', '2021-10-18', '2021-10-25', '2021-11-01',\n",
       "               '2021-11-08', '2021-11-15', '2021-11-22', '2021-11-29',\n",
       "               '2021-12-06', '2021-12-13', '2021-12-20', '2021-12-27'],\n",
       "              dtype='datetime64[ns]', freq='W-MON')"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start=\"2021-1-1\", end=\"2021-12-31\", freq=\"W-MON\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61d2d98e-2661-4fa7-92d3-192f6dc2a80e",
   "metadata": {},
   "source": [
    "# 生成一天的所有小时"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "074e9dbd-05ae-49a6-a4bc-f035d188e5f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-10-01 00:00:00', '2021-10-01 01:00:00',\n",
       "               '2021-10-01 02:00:00', '2021-10-01 03:00:00',\n",
       "               '2021-10-01 04:00:00', '2021-10-01 05:00:00',\n",
       "               '2021-10-01 06:00:00', '2021-10-01 07:00:00',\n",
       "               '2021-10-01 08:00:00', '2021-10-01 09:00:00',\n",
       "               '2021-10-01 10:00:00', '2021-10-01 11:00:00',\n",
       "               '2021-10-01 12:00:00', '2021-10-01 13:00:00',\n",
       "               '2021-10-01 14:00:00', '2021-10-01 15:00:00',\n",
       "               '2021-10-01 16:00:00', '2021-10-01 17:00:00',\n",
       "               '2021-10-01 18:00:00', '2021-10-01 19:00:00',\n",
       "               '2021-10-01 20:00:00', '2021-10-01 21:00:00',\n",
       "               '2021-10-01 22:00:00', '2021-10-01 23:00:00'],\n",
       "              dtype='datetime64[ns]', freq='H')"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start=\"2021-10-1\", periods=24, freq=\"H\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "f5cfd502-b5cf-4cca-b4ef-4f7fde8383d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-10-01 00:00:00', '2021-10-01 01:00:00',\n",
       "               '2021-10-01 02:00:00', '2021-10-01 03:00:00',\n",
       "               '2021-10-01 04:00:00', '2021-10-01 05:00:00',\n",
       "               '2021-10-01 06:00:00', '2021-10-01 07:00:00',\n",
       "               '2021-10-01 08:00:00', '2021-10-01 09:00:00',\n",
       "               '2021-10-01 10:00:00', '2021-10-01 11:00:00',\n",
       "               '2021-10-01 12:00:00', '2021-10-01 13:00:00',\n",
       "               '2021-10-01 14:00:00', '2021-10-01 15:00:00',\n",
       "               '2021-10-01 16:00:00', '2021-10-01 17:00:00',\n",
       "               '2021-10-01 18:00:00', '2021-10-01 19:00:00',\n",
       "               '2021-10-01 20:00:00', '2021-10-01 21:00:00',\n",
       "               '2021-10-01 22:00:00', '2021-10-01 23:00:00'],\n",
       "              dtype='datetime64[ns]', freq='H')"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(start=\"2021-10-1\", freq=\"H\", end=\"2021-10-2\", closed=\"left\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
